package com.likui.myqq.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.likui.myqq.R;
import com.likui.myqq.activity.QQContactActivity;
import com.likui.myqq.adapter.CallAdapter;
import com.likui.myqq.task.AsyncTaskBase;
import com.likui.myqq.view.CustomListView;
import com.likui.myqq.view.LoadingView;

/**
 * Created by helikui on 2015/10/2.
 */
public class CallFragment extends Fragment {
    private Context mContext;
    private View mBaseView;
    private View mSearchView;
    private CustomListView mCustomListView;
    private LoadingView mLoadingView;
    private RelativeLayout rlView;
    private Button contact;
    private CallAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext = getActivity();
        mBaseView = inflater.inflate(R.layout.fragment_news_call, null);
        mSearchView = inflater.inflate(R.layout.common_search_xl, null);
        findView();
        init();
        return mBaseView;
    }

    private void findView() {
        mCustomListView = (CustomListView) mBaseView.findViewById(R.id.callListView);
        mLoadingView = (LoadingView) mSearchView.findViewById(R.id.loading);
        rlView = (RelativeLayout) mSearchView.findViewById(R.id.rl_call);
        contact = (Button) mSearchView.findViewById(R.id.btn_contact);
    }

    private void init() {
        mAdapter = new CallAdapter(mContext);
        mCustomListView.setAdapter(mAdapter);
        mCustomListView.addHeaderView(mSearchView);
        mCustomListView.setCanLoadMore(false);
        mCustomListView.setOnRefreshListener(new CustomListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new CallAsyncTask(mLoadingView).execute(0);
            }
        });

        contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, QQContactActivity.class);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.activity_up, R.anim.fade_out);
            }
        });
    }

    private class CallAsyncTask extends AsyncTaskBase {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(Integer integer) {
            super.onPostExecute(integer);
            if (integer == 1) {
                mCustomListView.onRefreshComplete();
                mAdapter.notifyDataSetChanged();
            }
            rlView.setVisibility(View.VISIBLE);
        }

        @Override
        protected Integer doInBackground(Integer... params) {
            int result = -1;
            try {
                Thread.sleep(2000);
                result = 1;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }

        public CallAsyncTask(LoadingView loadingView) {
            super(loadingView);
            rlView.setVisibility(View.GONE);
        }
    }
}
